package com.jumi.microservice.controller.api;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpResponse;
import com.jm.member.common.jwt.annon.IgnoreUrlsAnnon;
import com.jm.member.common.messages.entity.JiGuangResponse;
import com.jm.member.common.messages.util.JiGuangUtil;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.dto.reponse.api.LoginResponse;
import com.jumi.microservice.entity.JmUser;
import com.jumi.microservice.enums.MessageType;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/appLogin")
@Api(tags = "C-App登录接口")
public class AppLoginController extends LoginController {

    @PostMapping("/loginTokenVerify")
    @IgnoreUrlsAnnon("/loginTokenVerify")
    @ApiOperation("本机号码一键登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inviterId", value = "邀请人id"),
            @ApiImplicitParam(name = "registerId", value = "jpush设备id")
    })
    public ResponseResult loginTokenVerify(@ApiParam(required = true, value = "loginToken") @RequestParam String loginToken,
                                           String inviterId, String registerId) {
        Long memberInviterId =  (inviterId != null && inviterId.length() != 0) ?  Long.parseLong(inviterId) : 0;
        JiGuangResponse jGResponse = JiGuangUtil.loginTokenVerify(loginToken);
        if (jGResponse.getCode().equals(JiGuangResponse.successLoginTokenCode)) {
            String mobile = JiGuangUtil.decode(jGResponse.getPhone());
            LoginResponse response = mobileLogin(mobile, memberInviterId, registerId);
            return ResponseResult.success(response);
        }
        return ResponseResult.error(500, "一键登录失败", jGResponse);
    }


    @PostMapping("/checkWeChatBound")
    @IgnoreUrlsAnnon("/checkWeChatBound")
    @ApiOperation("验证微信是否绑定用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "inviterId", value = "邀请人id"),
            @ApiImplicitParam(name = "registerId", value = "jpush设备id")
    })
    public ResponseResult checkExist(@ApiParam(required = true, value = "微信unionId") @RequestParam String unionId,
                                     String inviterId, String registerId) {
        Long memberInviterId =  (inviterId != null && inviterId.length() != 0) ?  Long.parseLong(inviterId) : 0;
        JmUser jmUser = checkExistByUnionId(unionId);
        if (ObjectUtil.isEmpty(jmUser)) {
            return ResponseResult.error("用户未绑定");
        }
        LoginResponse response = login(jmUser, memberInviterId, registerId);
        return ResponseResult.success(response);
    }

    @PostMapping("/loginIOS")
    @IgnoreUrlsAnnon("/loginIOS")
    @ApiOperation("验证IOS是否绑定用户并登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "iosSignId", value = "ios登录唯一标识码", required = true),
            @ApiImplicitParam(name = "inviterId", value = "邀请人id"),
            @ApiImplicitParam(name = "registerId", value = "jpush设备id")
    })
    public ResponseResult loginIOS(String iosSignId, String inviterId, String registerId) {
        Long memberInviterId =  (inviterId != null && inviterId.length() != 0) ?  Long.parseLong(inviterId) : 0;
        JmUser jmUser = checkExistByIOSId(iosSignId);
        if (ObjectUtil.isEmpty(jmUser)) {
            return ResponseResult.error("用户未绑定");
        }
        LoginResponse response = login(jmUser, memberInviterId, registerId);
        return ResponseResult.success(response);
    }

    @PostMapping("/registerAndLoginIOS")
    @IgnoreUrlsAnnon("/registerAndLoginIOS")
    @ApiOperation("通过关联IOS唯一标识码与手机号注册并登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "手机号"),
            @ApiImplicitParam(name = "code", value = "短信验证码"),
            @ApiImplicitParam(name = "iosSignId", value = "ios登录唯一标识码", required = true),
            @ApiImplicitParam(name = "inviterId", value = "邀请人id"),
            @ApiImplicitParam(name = "registerId", value = "jpush设备id")
    })
    public ResponseResult registerAndLoginIOS(String mobile, String code, String loginToken, String iosSignId, String inviterId, String registerId) {
        Long memberInviterId =  (inviterId != null && inviterId.length() != 0) ?  Long.parseLong(inviterId) : 0;
        if (ObjectUtil.isAllNotEmpty(mobile, code)) {
            // 手机号-验证码登录
            CheckSms(mobile, code, MessageType.登录验证);
        } else if (ObjectUtil.isNotEmpty(loginToken)) {
            // 手机号一键登录
            JiGuangResponse jGResponse = JiGuangUtil.loginTokenVerify(loginToken);
            if (jGResponse.getCode().equals(JiGuangResponse.successLoginTokenCode)) {
                mobile = JiGuangUtil.decode(jGResponse.getPhone());
            }else{
                return ResponseResult.error(500, "一键登录失败", jGResponse);
            }
        } else {
            // 登录信息缺失
            ResponseResult.error("登录信息缺失!");
        }
        JmUser userByMobile = userService.getUserByMobile(mobile);
        if(ObjectUtil.isEmpty(userByMobile.getIosSignId()))
            return ResponseResult.error(500, "该手机号已绑定ios识别码,请重新选择手机号!");
        if (ObjectUtil.isEmpty(userByMobile)) {
            userByMobile = new JmUser();
            userByMobile.setMobile(mobile);
        }
        userByMobile.setIosSignId(iosSignId);
        LoginResponse response = login(userByMobile, memberInviterId, registerId);
        return ResponseResult.success(response);
    }


}
